package com.meihua.baoleyi.ui;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.meihua.baoleyi.R;
import com.meihua.baoleyi.base.BaseActivity;
import com.meihua.baoleyi.common.APIServer;
import com.meihua.baoleyi.common.MyApp;
import com.meihua.baoleyi.common.PrefsHelper;
import com.meihua.baoleyi.dialog.ClearCacheDialog;
import com.meihua.baoleyi.utils.CleanMessageUtil;
import com.meihua.baoleyi.utils.SharedPreUtils;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 设置
 * Created by liming on 2017/10/17.
 */

public class SettingActivity extends BaseActivity {

    @Bind(R.id.text_back)
    ImageView ivSet;
    @Bind(R.id.text_title)
    TextView textTitle;
    @Bind(R.id.identity_safe)
    RelativeLayout identitySafe;
    @Bind(R.id.help_center)
    RelativeLayout helpCenter;
    @Bind(R.id.clear_unused)
    RelativeLayout clearUnused;
    @Bind(R.id.about_us)
    RelativeLayout aboutUs;
    @Bind(R.id.exit_login)
    Button exitLogin;
    String user_id, user_type, user;
    @Bind(R.id.tv_clear)
    TextView tvClear;
    private Context mContext;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        mContext = this;
        ButterKnife.bind(this);
        textTitle.setText("设置");
        user_id = SharedPreUtils.getString("user_id", "", this);
        user_type = SharedPreUtils.getString("user_type", "", this);
        user = user_type + "666666666" + user_id;
        try {
            String cacheSize = CleanMessageUtil.getTotalCacheSize(getApplicationContext());
            tvClear.setText(cacheSize);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @OnClick({R.id.text_back, R.id.identity_safe, R.id.help_center, R.id.clear_unused, R.id.about_us})
    public void onViewClicked(View view) {
        Intent in = new Intent();
        switch (view.getId()) {
            case R.id.text_back:
                //返回
                finish();
                break;
            case R.id.identity_safe:
                //账号与安全
                in.setClass(SettingActivity.this, IdentitySafeActivity.class);
                startActivity(in);
                break;
            case R.id.help_center:
                //帮助中心
                Intent intent = new Intent(this, CommonWebActivity.class);
                intent.putExtra("url", APIServer.PROBLEM);
                startActivity(intent);
                break;
            case R.id.clear_unused:
                //清楚缓存
                ClearCacheDialog dlg = new ClearCacheDialog.Builder(this)
                        .setMessage("确定清除缓存吗？")
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                dialogInterface.dismiss();
                                CleanMessageUtil.clearAllCache(getApplicationContext());
                                Toast.makeText(SettingActivity.this, "清除缓存成功", Toast.LENGTH_SHORT).show();
                                tvClear.setText("0KB");
                            }
                        })
                        .setNativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                dialogInterface.dismiss();
                            }
                        })
                        .build();

                dlg.show();
                break;
            case R.id.about_us:
                //关于我们  打开关于我们的界面
                Intent intent1 = new Intent(this, CommonWebActivity.class);
                intent1.putExtra("url", APIServer.AbOUT_US+MyApp.getInstance().getToken());
                startActivity(intent1);
                break;
        }
    }

    @OnClick(R.id.exit_login)
    public void onViewClicked() {
        /*退出登录*/
        MyApp.getInstance().exit();
        myApp.logout(); //退出成功后清除用户有关的信息
        PrefsHelper.removeLoginInfo(); //清除保存的登入信息
        startActivity(new Intent(SettingActivity.this, LoginActivity.class));
    }
}
